﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;

namespace BLL.PayRecordsTransaction
{
    /// <summary>
    /// 统计消费记录业务对象
    /// </summary>
    public class StatsPayRecordsTransaction : QueryPayRecordsTransaction
    {
        private IList<int> recordsOIDs;

        public StatsPayRecordsTransaction(IList<int> _RecordsOIDs)
        {
            recordsOIDs = _RecordsOIDs;
        }

        public PayStatisticsObject PayStatistics
        {
            get 
            {
                base.Execute();
                IList<T_PayRecordsObject> newPayRecords = new List<T_PayRecordsObject>();
                newPayRecords = base.payRecords.Where(x => recordsOIDs.Contains(x.Db_OID)).ToList();
                PayStatisticsObject result = new PayStatisticsObject();

                //求出最开始日期
                result.BeginDate= newPayRecords.Min(x => x.Db_PayDate);
                //求出最后日期
                result.EndDate = newPayRecords.Max(x => x.Db_PayDate);
                //求出跨越天数
                TimeSpan ts = result.EndDate - result.BeginDate;
                result.CrossingDays = ts.Days;
                //求出消费总额
                result.PayTotal = newPayRecords.Sum(x => x.Db_PaySum);
                //求出平均日消费
                if (result.BeginDate.Equals(result.EndDate))
                {
                    result.AvgDayPaySum = result.PayTotal;
                    result.CrossingDays = 1;
                }
                else
                {
                    result.AvgDayPaySum = result.PayTotal / result.CrossingDays;
                }
                //求出必要消费笔数
               result.NecessaryPays = newPayRecords.Where(x => x.Db_PayNecessity == 1).ToList().Count;
                //求出可花可不花消费笔数
               result.NoNecessaryPays = newPayRecords.Where(x => x.Db_PayNecessity == 2).ToList().Count;
                //求出浪费消费笔数
               result.WastefulPays = newPayRecords.Where(x => x.Db_PayNecessity == 3).ToList().Count;

               return result;
            }
        }
    }
}
